import component from "../Component.js"
import zTreeUtils from "../../experience/utils/zTreeUtils.js";
import {LayerType} from "../../experience/db/SafeMonitorType.js"
export default class SensorRelationAlarm extends component {
    constructor() {
        super()
    }

    control() {
        this.ztreeUtils = new zTreeUtils()
        this.emitter.on('sm-alarm-relation',async () => {
            const sensor_node_id = this.emitter.params[0]
            const relation_broadcasts = this.links[sensor_node_id]?.speakers
            const relation_videos = this.links[sensor_node_id]?.videos
            // 将关联的所有关联广播站 都进行报警
            this.broadcastRelationAlarm(relation_broadcasts)
            this.videoRelationAlarm(relation_videos)
            this.PoweredAreaRelationAlarm(sensor_node_id)
        })
        // 接受 从外部传过来的 传感器关联关系的数据
        this.emitter.on('integration-receive',() => {
            const links = this.emitter.params[0]
            this.links = links
        })
        // 接受 从外部传过来的 断电区域的数据（里面包括了 关联的断馈电传感器）
        this.emitter.on('powered-areas-receive',() => {
            const areasinfo = this.emitter.params[0]
            console.log('报警里的 areas',areasinfo)
            this.areas = areasinfo
        })
    }

    template() {

    }

    // 传感器触发报警时候，关联的广播设备要发生的变化 ,par广播站数组
    broadcastRelationAlarm(speakers) {
        if(speakers) {
            const features = this.drawer.source(LayerType.SMBroadCast).getFeatures()
            const relationSpeakers = features.filter((el) => {
                return speakers.some((item) => {
                    return el.get('node_id') == item
                })
            })
            relationSpeakers.forEach((speaker) => {
                const coordinates = speaker.getGeometry().getCoordinates()
                const denormalCoordinate = [this.algo.denormalX(coordinates[0]),this.algo.denormalY(coordinates[1])]
                this.ztreeUtils.highLightAnimation(denormalCoordinate,3000,'warming')
            })


        }

    }

    // 传感器触发报警时候，关联的摄像头要发生的变化, par 摄像头数组
    videoRelationAlarm(videos) {
        if(videos) {
            const features = this.drawer.source(LayerType.SMVideo).getFeatures()
            const relationVideos = features.filter((el) => {
                return videos.some((item) => {
                    return el.get('node_id') == item
                })
            })
            relationVideos.forEach((video) => {
                const coordinates = video.getGeometry().getCoordinates()
                const denormalCoordinate = [this.algo.denormalX(coordinates[0]),this.algo.denormalY(coordinates[1])]
                this.ztreeUtils.highLightAnimation(denormalCoordinate,3000,'warming')
            })
        }

    }

    // 传感器触发报警时候，关联的断电区域要发生的变化, par 关联断电区域数组
    PoweredAreaRelationAlarm(node_id) {
        if(!this.areas) {
            return
        }
        const availibleAreas = this.areas.filter((el) => {
            return el.nodes == node_id
        }).map((item) => {return item.id})
        const areaFeatures = this.drawer.source(LayerType.SMPoweredArea).getFeatures()
        areaFeatures.forEach((f) => {
            if(availibleAreas.some((a) => {
                return a == f.get('id')
            })){
                const style = f.getStyle()
                style.setFill(new ol.style.Fill({
                    color: 'rgba(255, 0, 0, 0.3)'
                }))
                style.setStroke(new ol.style.Stroke({
                    color: 'orange'
                }))
                f.setStyle(style)
            }
        })

    }




}

